/**
 * @author: MaLeLe
 * @date: 2024/3/28 10:38
 */




public class _08_MyAtoi {
    /**
     *
     *@param [s]
     *@return int
     *@throws 
     **/
    public int myAtoi(String s) {
        s = s.trim();
        if (s.isEmpty()) {
            return 0;
        }

        int sign = 1;
        if (s.charAt(0) == '+' || s.charAt(0) == '-') {
            sign = s.charAt(0) == '+' ? 1 : -1;
            s = s.substring(1);
        }

        long result = 0;
        for (char c : s.toCharArray()) {
            if (!Character.isDigit(c)) {
                break;
            }
            result = result * 10 + (c - '0');
            if (result > Integer.MAX_VALUE) {
                break;
            }
        }

        result = sign * result;
        result = Math.max(Math.min(result, Integer.MAX_VALUE), Integer.MIN_VALUE);

        return (int) result;
    }


}
